import { connect } from 'react-redux';
import { InputNumber, message } from 'antd';
import emitter from '@/events';
import { getCartListData } from '@/store/actions/cart';
// css
import styles from '@/styles/CommonDetail.module.scss';

// 加入购物车
const Cart = ({_getCartListData, cart: { datePickerOpen }, data, isLimit, minNum, maxNum, onAddOrder, onChangeNum, onAddCart }) => {
    
    const { stock, bedType } = data;
    const addCart = function (e){
        // 第一次加入购物车前，提示用户先选择时间
        if(datePickerOpen){
            bedType ? message.warn('请先确认入住/退房日期') : message.warn('请先确认出行日期');
            emitter.emit('openDatePicker', datePickerOpen);
            return false
        }
        onAddCart(e, data);
        setTimeout(() => {
            _getCartListData()
        }, 1500)
    }
    return (
        <>
            <button className={styles['buy']} onClick={() => { onAddOrder(data) }}>立即预订</button>
            <button className={styles['cart']} disabled={!stock || isLimit}>
                <span>加入购物车</span>
                <div className={styles['cart-box']}>
                    <InputNumber min={minNum} max={maxNum} value={data.cartNum} onChange={(v) => { onChangeNum(v, data.id) }} />
                    <span onClick={(e) => { addCart(e) }}>确定</span>
                </div>
            </button>
        </>
    )
}

const mapStateToProps = state => {
    return {
        cart: state.cart
    }
}
const mapDispatchToProps = dispatch => {
    return {
        _getCartListData: v => dispatch(getCartListData(v)),
    }
}
export default connect(mapStateToProps, mapDispatchToProps)(Cart);